package com.wingscode.modules.admin.controller;

import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.modules.common.entity.AgentEntity;
import com.wingscode.modules.common.entity.UserEntity;
import com.wingscode.modules.common.service.AgentService;
import com.wingscode.modules.common.service.UserService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 用户
 *
 * @author yan
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AgentService agentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("admin:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:user:info")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);
        if (user != null && user.getAgentId() != null) {
            AgentEntity agent = agentService.getById(user.getAgentId());
            if (agent != null)
                user.setAgentName(agent.getCompany());
        }

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:user:save")
    public R save(@RequestBody UserEntity user) {
        user.setCreateTime(new Date());
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:user:update")
    public R update(@RequestBody UserEntity user) {
        user.setUpdateTime(new Date());
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:user:delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/auditAuth")
    public R auditAuth(@RequestParam Long id,@RequestParam int status) {
        UserEntity userEntity=userService.getById(id);
        userEntity.setUpdateTime(new Date());
        userService.updateById(userEntity);
        return R.ok(200, "");

    }
    
    /**
     * 设为管理员
     */
    @RequestMapping("/setRole")
    public R setRole(@RequestBody Long id) {
        userService.setRoleUpdate(id);

        return R.ok();
    }
    
    /**
     * 取消管理员
     */
    @RequestMapping("/cancelRole")
    public R cancelRole(@RequestBody Long id) {
        userService.cancelRoleUpdate(id);

        return R.ok();
    }

}
